package com.example.trackcircuitdiagnosis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.trackcircuitdiagnosis.entity.AreaEntity;
import com.example.trackcircuitdiagnosis.service.AreaSerivce;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController

@RequestMapping("/area")
@CrossOrigin
public class AreaController {


    @Autowired
    private AreaSerivce areaSerivce;

    // 新增或者更新
    @PostMapping
    @Operation(description = "保存区域信息")

    public boolean save(@RequestBody AreaEntity area) {
        return areaSerivce.saveOrUpdate(area);
    }

    @DeleteMapping("/{id}")
    @Operation(
            description = "删除区域信息",
            parameters = {
                    @Parameter(name = "id", description = "要删除的区域信息id")
            }

    )
    public Boolean delete(@PathVariable Integer id) {
        return areaSerivce.removeById(id);
    }

    @PostMapping("/del/batch")
    @Operation(
            description = "批量删除区域",
            parameters = {
                    @Parameter(name = "ids", description = "要批量删除的区域信息")
            }

    )
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return areaSerivce.removeByIds(ids);
    }

    @GetMapping
    @Operation(description = "查找全部")
    public List<AreaEntity> findAll() {
        return areaSerivce.list();
    }

    @GetMapping("/{id}")

    @Operation(
            description = "查找区域信息",
            parameters = {
                    @Parameter(name = "id", description = "要查找区域信息id")
            }

    )
    public AreaEntity findOne(@PathVariable Integer id) {
        return areaSerivce.getById(id);
    }

    @GetMapping("/page")
    @Operation(
            description = "分页查询",
            parameters = {
                    @Parameter(name = "pageNum", description = "要查找区域信息页数"),
                    @Parameter(name = "pageSize", description = "要查找区域信息每一页的数量")
            }

    )
    public Page<AreaEntity> findPage(@RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize) {
        QueryWrapper<AreaEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return areaSerivce.page(new Page<>(pageNum, pageSize), queryWrapper);
    }







}

